﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class TailingpondBackWaterReposity : Reposity
    {
        public tailingpond_backwater_info GetDailyBackwaterInfo(int tailingpondId, DateTime date)
        {
            var backwater = from s in _dataContext.tailingpond_backwater_infos
                             where tailingpondId == s.tailingpond_id && s.date == date.Date
                             select s;

            if (backwater != null && backwater.Count() > 0)
            {
                return backwater.First();
            }
            else
            {
                return null;
            }
        }

        public IDictionary<DateTime, tailingpond_backwater_info> QueryBackWaterInfo(int tailingpondId, DateTime dateFrom, DateTime dateTo)
        {
            var dailywater = from s in _dataContext.tailingpond_backwater_infos
                             where tailingpondId == s.tailingpond_id && s.date >= dateFrom && s.date < dateTo
                             select s;

            IDictionary<DateTime, tailingpond_backwater_info> infos = new Dictionary<DateTime, tailingpond_backwater_info>();
            foreach (tailingpond_backwater_info info in dailywater)
            {
                infos.Add(info.date, info);
            }

            return infos;
        }

        public tailingpond_backwater_info CreateBackWaterInfo(tailingpond_backwater_info backwater)
        {
            _dataContext.tailingpond_backwater_infos.InsertOnSubmit(backwater);
            SubmitChanges();
            return backwater;
        }

        public bool DeleteBackWaterInfo(tailingpond_backwater_info backwater)
        {
            _dataContext.tailingpond_backwater_infos.DeleteOnSubmit(backwater);
            return true;
        }
    }
}